$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/notice/list',
        datatype: "json",
        colModel: [			
			{ label: '通知id', name: 'sysNoticeNo',  width: 100,align:"center", key: true },
			{ label: '通知标题', name: 'titles',align:"center", width: 120 },
			{ label: '内容', name: 'content',align:"center", width: 120,formatter:function (value,options,row) {
                    return value.length==0?'无内容':'<a  onclick ="openContent('+JSON.stringify(row).replace(/"/g, '&quot;')+')"> 内容预览 </a>';
                } },
			{ label: '封面图片', name: 'coverImg',align:"center", width: 60,formatter:function (value,option,row) {
                    return value==null?'无图片':'<a  onclick = "openImg(\''+value+'\')"> 查看图片 </a>';
                } },
			{ label: '是否显示', name: 'isShow',align:"center", width: 60 },
			{ label: '通知时间', name: 'createDate', align:"center", width: 85},
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect:true,
        multiboxonly:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.getDeptId();
});


var ue=UE.getEditor('editor');

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            phone: null,
            name:null,
            deptId:null
        },
        currDeptId:null,
        showList: true,
        showAdd:false,
        title:null,
        notice:{
            titles:null,
            content:null,
        }

    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean:function(){
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('setGridParam',{'page':1});
            vm.reload();
        },
        //获得当前登录人的渠道id
        getDeptId:function () {
            $.get(baseURL + "sys/user/info", function (r) {
                vm.currDeptId = r.user.deptId;
            })
        },
        reload: function () {
            vm.getDeptId();
            console.log(vm.currDeptId);
            vm.showList = true;
            vm.showAdd=false;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'deptId':vm.currDeptId},
                page:page
            }).trigger("reloadGrid");
        },
        insert:function () {
            vm.showList=false;
            vm.showAdd=true;
            vm.title="添加消息"
        },
        add:function () {

            let formdata = new FormData($('.addnotice')[0]);

            formdata.append("coverImg",$("input[name='file']"));

            formdata.append('titles',$(".addnotice input[type='text']"));
            //为图片添加宽度自适应
            let content=ue.getContent();
            content=content.replace("<img","<img width='100%' height='auto'");
            formdata.append('content',content);

            console.log(formdata);
            $.ajax({
                url: baseURL+'sys/notice/add',
                type: 'POST',
                datatype: 'json',
                data: formdata,
                cache:false,
                traditional: true,
                contentType: false,
                processData: false,
                success: function (r) {
                    if (r.code==0){
                        alert("成功");
                        vm.reload();
                    }
                }
            });
        }
        ,del:function () {
            let rowid=getSelectedRow();
            let row = $("#jqGrid").jqGrid('getRowData',rowid);
            if(row==null){
                return;
            }
            confirm("确定要删除该消息么？",function () {
                $.getJSON(baseURL + "sys/notice/delete/" + row.sysNoticeNo, function (r) {
                    vm.reload();
                })

            })
        }
    }
});

//打开图片
function openImg(src){

    $("#goodsImg").attr("src",src);

    layer.open({
        type:1,
        title:"请选择",
        content:$("#goodsImg"),
        btn:['确定'],
        area:['720px','640px'],
        btn1:function (index) {
            layer.close(index);
        }

    })
}

//弹窗显示内容
function  openContent(row) {
    $("#noticeContent").html(row.content);
    layer.open({
        type:1,
        title:"内容预览",
        content:$("#noticeContent"),
        btn:['确定'],
        area:['392px','716px'],
        btn1:function (index) {
            $("#content").html("");
            layer.close(index);
        }

    })

}